// stores/publicInfo.js 定义一些全局参数变量
import { defineStore } from 'pinia'
import middleApi from '@/api/middleApi.js'

export const usePublicStore = defineStore('publicInfo', {
  state: () => {
    return {
      isCollapse: false, //左侧菜单栏是否展开
      canteenDto: {}, //中间表数据
    }
  },
  getters: {
    //工资范围
    hcmPayGroupEntities: (state) => state.canteenDto.hcmPayGroupEntities || [],
    // 用工性质
    hcmEmploymentNatureEntities: (state) => state.canteenDto.hcmEmploymentNatureEntities || [],
    // 员工组
    hcmEmployeeClassEntities: (state) => state.canteenDto.hcmEmployeeClassEntities || [],
    // 食堂组织
    sysAreaEntityList: (state) => state.canteenDto.sysAreaEntityList || [],
  },
  actions: {
    //获取中间表数据
    async getMiddleTable() {
      try {
        const res = await middleApi.middleTable({
          t: +new Date(),
        })
        if (res.code === 0) {
          //缓存中间表数据
          this.canteenDto = res.canteenDto || {}
          return this.canteenDto
        }
        throw new Error(res.msg || '获取中间表信息失败')
      } catch (error) {
        console.error(error.message || '获取中间表信息失败！')
        throw error
      }
    },
    setIsCollapse(bool) {
      this.isCollapse = bool
    },
  },
  persist: {
    storage: localStorage,
  },
})
